<template>
  <div class="page-user-order">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        label-width="auto"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="stockBillNo"
            :label="$t('sys.nickName')+':'"
          >
            <el-input
              v-model="searchForm.nickName"
              type="text"
              :placeholder="$t('sys.nickName')"
              clearable
            />
          </el-form-item>
          <el-form-item :label="$t('order.buyTime')+':'">
            <el-date-picker
              v-model="createDateRange"
              type="datetimerange"
              :range-separator="$t('date.tip')"
              value-format="YYYY-MM-DD HH:mm:ss"
              :start-placeholder="$t('text.startTime')"
              :end-placeholder="$t('date.end')"
              :default-time="defaultTime"
              @change="onCreateTimeChange"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch(searchFormRef)"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con">
        <el-table
          ref="dataListRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <el-table-column
            :label="$t('sys.nickName')"
            prop="nickName"
            align="left"
            width="190"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.nickName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="pic"
            :label="$t('publics.profilePicture')"
          >
            <template #default="scope">
              <div class="table-cell-image">
                <img
                  v-if="!scope.row.pic"
                  src="~@/assets/img/userImg.jpg"
                  style="width:130px;"
                  alt=""
                >
                <img
                  v-else
                  :src="resourcesUrl +scope.row.pic"
                  alt=""
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('order.buyLevel')"
            prop="levelName"
            align="left"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.levelName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('user.memberTime')"
            prop="term"
            align="left"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.term }}{{ termType(scope.row.termType) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('order.buyTime')"
            prop="createTime"
            align="left"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.createTime }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('order.payAmount')"
            prop="payAmount"
            align="left"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.payAmount }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('order.paymentMethod')"
            prop="stockBillType"
            align="left"
            :show-overflow-tooltip="true"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">
                <span v-if="scope.row.isPayed === 0">{{
                  $t("order.unpaid")
                }}</span>
                <span v-else>{{
                  [
                    $t("order.pointsPayment"),
                    $t("order.wecProPay"),
                    $t("order.alipayPCPayment"),
                    $t("order.wechatScanCodePayment"),
                    $t("order.wechatH5Payment"),
                    $t("order.weclAccountPay"),
                    $t("order.alipayH5Payment"),
                    $t("order.alipayAPPPayment"),
                    $t("order.wechatAPPPayment"),
                    $t("order.balancePayment"),
                    $t("order.payPalPayment")
                  ][scope.row.payType]
                }}</span>
              </span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>
  </div>
</template>

<script setup>
const resourcesUrl = import.meta.env.VITE_APP_RESOURCES_URL

const termType = (val) => {
  switch (val) {
    case 1:
      return $t('coupon.day')
    case 2:
      return $t('user.week')
    case 3:
      return $t('user.month')
    case 4:
      return $t('user.season')
    case 5:
      return $t('user.year')
  }
}

const searchForm = ref({
  nickName: '',
  createStartTime: null, // 制单起始时间
  createEndTime: null, // 制单结束时间
  stockBillItemType: 1
}) // 搜索
const page = ref({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
onMounted(() => {
  onGetDataList(page.value)
})

const dataListLoading = ref(false)
let tempSearchForm = null // 保存上次点击查询的请求条件
const dataList = ref([])
const onGetDataList = (pageParam, newData = false) => {
  dataListLoading.value = true
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm.value))
  }
  http({
    url: http.adornUrl('/user/userLevelLog/pageBuyLevelLog'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.value.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.value.pageSize : pageParam.pageSize
        },
        tempSearchForm
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.value.total = data.total
    dataListLoading.value = false
  }).catch(() => {
    dataListLoading.value = false
  })
}

const onSearch = (newData = false) => {
  onGetDataList(page.value, newData)
}
const onPageSizeChange = (val) => {
  page.value.pageSize = val
  onGetDataList(page.value)
}
const onPageChange = (val) => {
  page.value.currentPage = val
  onGetDataList(page.value)
}
const defaultTime = ref([
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59)
])
const createDateRange = ref([]) // 购买时间范围
const searchFormRef = ref(null)
const onResetSearch = (formName) => {
  formName.resetFields()
  searchForm.value.nickName = ''
  searchForm.value.createStartTime = null
  searchForm.value.createEndTime = null
  createDateRange.value = []
}
const onCreateTimeChange = () => {
  if (!createDateRange.value || createDateRange.value.length === 0) {
    searchForm.value.createStartTime = null
    searchForm.value.createEndTime = null
  } else {
    searchForm.value.createStartTime = createDateRange.value[0]
    searchForm.value.createEndTime = createDateRange.value[1]
  }
}

</script>

<style scoped lang="scss">
.page-user-order{
div :deep(.el-tabs__item.is-active){
  color:#000;
  font-weight: 800;
}
div :deep(.el-col-md-6){
  width:30%
}
div :deep(.table-cell-text){
  display: inline !important;
}
}

</style>
